package com.loklok.code.confuse.ui;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.loklok.code.confuse.service.FullUpdateTemplateService;
import com.loklok.code.confuse.service.GenerateUrlMappingService;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.util.concurrent.CompletableFuture;

public class UrlMappingDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextArea batchUrlsArea;

    public UrlMappingDialog(AnActionEvent anActionEvent) {
        setContentPane(contentPane);
        setModal(true);
        setResizable(true);
        setSize(800, 500);
        setLocation(600, 200);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(e -> {
            String urlsAreaText = batchUrlsArea.getText();
            buttonOK.setEnabled(false);
            if (StringUtils.isEmpty(urlsAreaText)) {
                buttonOK.setEnabled(true);
                Messages.showMessageDialog("接口url不能为空!", "提示", Messages.getWarningIcon());
                return;
            }
            CompletableFuture.runAsync(() -> GenerateUrlMappingService.generateUrlMapping(anActionEvent, urlsAreaText), FullUpdateTemplateService.EXECUTOR);
            onOK();
        });
    }

    private void onOK() {
        // add your code here
        dispose();
    }
}
